<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2022/2/17
 * Time: 13:37
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Bogoods extends Base
{
    /**
     * 首页
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $keyword = trim(input('keyword'), ' ');
            $bg_first_cate = input('bg_first_cate');
            $bg_second_cate = input('bg_second_cate');
            $bg_status = input('bg_status');
            $bg_type = input('bg_type');
            $bg_recommend = input('bg_recommend');
            $where['bg_status'] = ['<>', 3];
            // 关键字
            if(isset($keyword) && $keyword != ''){
                $where['bg_id|bg_name'] = ['like', '%' . $keyword . '%'];
            }
            // 一级分类
            if(isset($bg_first_cate) && $bg_first_cate != ''){
                $where['bg_first_cate'] = $bg_first_cate;
            }
            // 二级分类
            if(isset($bg_second_cate) && $bg_second_cate != ''){
                $where['bg_second_cate'] = ['LIKE', '%,' . $bg_second_cate . ',%'];
            }
            // 状态
            if(isset($bg_status) && $bg_status != ''){
                $where['bg_status'] = $bg_status;
            }
            if(isset($bg_type) && $bg_type != ''){
                $where['bg_type'] = $bg_type;
            }
            if(isset($bg_recommend) && $bg_recommend != ''){
                $where['bg_is_commend'] = $bg_recommend;
            }
        
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_bo_goods')
                ->join('up_bo_goods_cate u1', 'u1.bgc_id = bg_first_cate', 'LEFT')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::table('up_bo_goods')
                ->join('up_bo_goods_cate u1', 'u1.bgc_id = bg_first_cate', 'LEFT')
                ->field('bg_id, bg_name, bg_thumb, bg_price, bg_sale_number, bg_status, bg_spec, 
                bg_stock, bg_time, u1.bgc_name first_cate_name, bg_second_cate, bg_is_commend, bg_sort, bg_old_price, bg_type')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('bg_sort desc, bg_time desc')
                ->select();
            if($lists){
                foreach ($lists as $k => $v){
                    $second_cate_ids = trim($v['bg_second_cate'], ',');
                    $second_cate = Db::table('up_bo_goods_cate')->where('bgc_id', 'IN', $second_cate_ids)->column('bgc_name');
                    $lists[$k]['bg_second_cate'] = implode(',', $second_cate);
                }

            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $cate = Db::table('up_bo_goods_cate')->where('bgc_level', 1)->field('bgc_id, bgc_name')->select();
        $this->assign([
            'cate' => $cate
        ]);
        return $this->fetch();
    }
    
    /**
     * 添加商品
     * @return mixed|\think\response\Json
     */
    public function goodsAdd(){
        if(Request::instance()->isPost()){
            $bg_name = trim(input('bg_name'), ' ');
            $bg_thumb = input('bg_thumb');
            $bg_images = input('bg_images');
            $bg_status = input('bg_status');
            $bg_first_cate = input('bg_first_cate');
            $bg_second_cate = input('select');
            $bg_price = input('bg_price');
            $bg_old_price = input('bg_old_price');
            $bg_spec = input('bg_spec');
            $bg_stock = input('bg_stock');
            $bg_content = input('bg_content');
            $bg_product_code = trim(input('bg_product_code'));
            $bg_is_commend = input('bg_is_commend');
            $bg_sort = input('bg_sort') ?: 1;
//            $bg_show_salenumber = input('bg_show_salenumber');
            $bg_type = input('bg_type');
            
            if($bg_first_cate == ''){
                return json(['code' => 100, 'msg' => '请选择商品一级分类']);
            }

            $has = Db::table('up_bo_goods')->where('bg_product_code', $bg_product_code)->find();
            if($has) return json(['code' => 100, 'msg' => '产品码已存在']);

            if($bg_second_cate) $bg_second_cate = ',' . $bg_second_cate . ',';

            $insert = [
                'bg_name' => $bg_name,
                'bg_thumb' => $bg_thumb,
                'bg_content' => $bg_content,
                'bg_images' => $bg_images,
                'bg_status' => $bg_status,
                'bg_first_cate' => $bg_first_cate,
                'bg_second_cate' => $bg_second_cate,
//                'bg_show_salenumber' => $bg_show_salenumber,
                'bg_price' => $bg_price,
                'bg_old_price' => $bg_old_price,
                'bg_spec' => $bg_spec,
                'bg_stock' => $bg_stock,
                'bg_is_commend' => $bg_is_commend,
                'bg_product_code' => $bg_product_code,
                'bg_sort' => $bg_sort,
                'bg_type' => $bg_type,
                'bg_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_bo_goods')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        $cate = Db::table('up_bo_goods_cate')->where('bgc_level', 1)->field('bgc_id, bgc_name')->select();
        $this->assign([
            'cate' => $cate
        ]);
        return $this->fetch('bogoods/goodsadd');
    }
    
    /**
     * 详情
     * @return mixed
     */
    public function goodsDetail(){
        $bg_id = input('bg_id');
        $goods = Db::table('up_bo_goods')
            ->join('up_bo_goods_cate u1', 'u1.bgc_id = bg_first_cate', 'LEFT')
            ->field('bg_id, bg_name, bg_thumb, bg_price, bg_sale_number, bg_status, bg_spec, bg_stock, 
            bg_time, u1.bgc_name first_cate_name, bg_images, bg_content, bg_show_salenumber, 
            bg_show_stock, bg_product_code, bg_second_cate, bg_is_commend, bg_old_price, bg_type')
            ->where('bg_id', $bg_id)
            ->find();
        if($goods['bg_images']){
            $goods['bg_images'] = explode(',', $goods['bg_images']);
        }else{
            $goods['bg_images'] = [];
        }
        $second_str = trim($goods['bg_second_cate'], ',');
        $second_cate_name = Db::table('up_bo_goods_cate')->where('bgc_id', 'IN', $second_str)->column('bgc_name');
        $goods['second_cate_name'] = implode(',', $second_cate_name);
        return $this->fetch('bogoods/goodsdetail', ['goods' => $goods]);
    }
    
    /**
     * 编辑商品
     * @return mixed|\think\response\Json
     */
    public function goodsEdit(){
        $bg_id = input('bg_id');
        if(Request::instance()->isPost()){
            $bg_name = trim(input('bg_name'), ' ');
            $bg_thumb = input('bg_thumb');
            $bg_images = input('bg_images');
            $bg_status = input('bg_status');
            $bg_first_cate = input('bg_first_cate');
            $bg_second_cate = input('select');
            $bg_price = input('bg_price');
            $bg_old_price = input('bg_old_price');
            $bg_spec = input('bg_spec');
            $bg_stock = input('bg_stock');
            $bg_content = input('bg_content');
            $bg_show_salenumber = input('bg_show_salenumber');
            $bg_show_stock = input('bg_show_stock');
            $bg_product_code = trim(input('bg_product_code'));
            $bg_is_commend = input('bg_is_commend');
            $bg_sort = input('bg_sort') ?: 1;
            $bg_type = input('bg_type');

            if($bg_first_cate == ''){
                return json(['code' => 100, 'msg' => '请选择商品一级分类']);
            }

            $has = Db::table('up_bo_goods')->where('bg_product_code', $bg_product_code)->find();
            if($has && $bg_id != $has['bg_id']) return json(['code' => 100, 'msg' => '产品码已存在']);

            if($bg_second_cate) $bg_second_cate = ',' . $bg_second_cate . ',';
            $update = [
                'bg_name' => $bg_name,
                'bg_thumb' => $bg_thumb,
                'bg_content' => $bg_content,
                'bg_images' => $bg_images,
                'bg_status' => $bg_status,
                'bg_first_cate' => $bg_first_cate,
                'bg_second_cate' => $bg_second_cate,
                'bg_show_salenumber' => $bg_show_salenumber,
                'bg_show_stock' => $bg_show_stock,
                'bg_price' => $bg_price,
                'bg_old_price' => $bg_old_price,
                'bg_spec' => $bg_spec,
                'bg_stock' => $bg_stock,
                'bg_is_commend' => $bg_is_commend,
                'bg_product_code' => $bg_product_code,
                'bg_sort' => $bg_sort,
                'bg_updatetime' => date('Y-m-d H:i:s'),
                'bg_type' => $bg_type
            ];
            $res = Db::table('up_bo_goods')->where('bg_id', $bg_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $goods = Db::table('up_bo_goods')
            ->where('bg_id', $bg_id)
            ->field('bg_name, bg_thumb, bg_images, bg_status, bg_first_cate, bg_second_cate, bg_price, 
            bg_spec, bg_stock, bg_content, bg_show_salenumber, bg_show_stock, bg_product_code, bg_is_commend, 
            bg_sort, bg_old_price, bg_type')
            ->find();
        $first_cate = Db::table('up_bo_goods_cate')->where('bgc_level', 1)->field('bgc_id, bgc_name')->select();
        $second_str = trim($goods['bg_second_cate'], ',');
//        $second_cate = Db::table('up_bo_goods_cate')->where('bgc_pid', 'IN', $goods['bg_first_cate'])->field('bgc_id value, bgc_name name')->select();
        $this->assign([
            'bg_id' => $bg_id,
            'goods' => $goods,
            'first_cate' => $first_cate,
//            'second_cate' => $second_cate,
            's_cate' => $second_str
        ]);
        return $this->fetch('bogoods/goodsedit');
    }
    
    /**
     * 上架
     * @return \think\response\Json
     */
    public function goodsOn(){
        $bg_id = input('id');
        $res = Db::table('up_bo_goods')->where('bg_id', $bg_id)->setField('bg_status', 1);
        if($res){
            return json(['code' => 200, 'msg' => '上架成功']);
        }else{
            return json(['code' => 100, 'msg' => '上架失败']);
        }
    }
    
    /**
     * 下架
     * @return \think\response\Json
     */
    public function goodsDown(){
        $bg_id = input('id');
        $res = Db::table('up_bo_goods')->where('bg_id', $bg_id)->setField('bg_status', 2);
        if($res){
            return json(['code' => 200, 'msg' => '下架成功']);
        }else{
            return json(['code' => 100, 'msg' => '下架失败']);
        }
    }

    /**
     * 设置推荐
     * @return \think\response\Json
     */
    public function goodsRecommend(){
        $bg_id = input('id');
        $res = Db::table('up_bo_goods')->where('bg_id', $bg_id)->setField('bg_is_commend', 2);
        if($res){
            return json(['code' => 200, 'msg' => '设置推荐成功']);
        }else{
            return json(['code' => 100, 'msg' => '设置推荐失败']);
        }
    }

    /**
     * 取消推荐
     * @return \think\response\Json
     */
    public function goodsUnrecommend(){
        $bg_id = input('id');
        $res = Db::table('up_bo_goods')->where('bg_id', $bg_id)->setField('bg_is_commend', 1);
        if($res){
            return json(['code' => 200, 'msg' => '取消推荐成功']);
        }else{
            return json(['code' => 100, 'msg' => '取消推荐失败']);
        }
    }
    
    /**
     * 删除商品
     * @return \think\response\Json
     */
    public function goodsDel(){
        $bg_id = input('id');
        $res = Db::table('up_bo_goods')->where('bg_id', $bg_id)->setField('bg_status', 3);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }

    /**
     * 排序
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function updateSort(){
        $id = input('id');
        $param = [
            'bg_sort'=>input('value'),
        ];
        $res = Db::table('up_bo_goods')->where('bg_id', $id)->update($param);
        if($res){
            return json(['code' => 200, 'msg' => '修改成功']);
        }else{
            return json(['code' => 100, 'msg' =>' 修改失败']);
        }
    }
}